package com.flymobi.service;

import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.po.Block;
import com.flymobi.util.QL;

public class BlockService extends CommonService {

	public List<Block> list(int startRow, int pagePerSize) {
		return dao.find(Block.class, QL.HQL.Block.LIST, new Object[] {},
				startRow, pagePerSize);
	}

	public List<Block> list(Long type, Long containerId, int startRow,
			int pagePerSize) {
		return dao.find(Block.class, QL.HQL.Block.LIST_BY_TYPE, new Object[] {
				type, containerId }, startRow, pagePerSize);
	}

	public Long getCount() {
		return dao.getCount(QL.HQL.Block.GET_COUNT, new Object[] {});
	}

	public void save(Block Block) {
		dao.save(Block);
	}

	public Block load(String id) {
		return dao.load(Block.class, new Long(id));
	}

	public Block get(String id) {
		return dao.get(Block.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}

	public void updateUp(String id) {
		Block pe = this.get(id);
		Block peUp = findUp(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	public void updateDown(String id) {
		Block pe = this.get(id);
		Block peUp = findDown(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	private void switchPosion(Block pe, Block peUp) {
		long pe1idx = pe.getIdx();
		pe.setIdx(peUp.getIdx());
		peUp.setIdx(pe1idx);
		dao.save(pe);
		dao.save(peUp);
	}

	private Block findUp(Block pe) {
		return dao.findSingle(Block.class, QL.HQL.Block.LIST_UP,
				new Object[] { pe.getContainerId(), pe.getIdx() });
	}

	private Block findDown(Block pe) {
		return dao.findSingle(Block.class, QL.HQL.Block.LIST_DOWN,
				new Object[] { pe.getContainerId(), pe.getIdx() });
	}
}
